/*
 * 创建日期 2009-11-11
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.html.query;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.cpp.core.bean.ParamContext;
import  org.fantasy.common.util.StringUtil;

/**
 *  范围输入框
 * @author: 王文成
 * @version: 1.0
 * @since 2009-11-11
 */
public class TextBetweenHtml extends AbstractQueryHtml {

	public TextBetweenHtml(HttpServletRequest request, ParamContext param) {
		super(request, param);
	}

	public String getHtml(){
		StringBuffer html = new StringBuffer(1024);
		String startName = getStartName();
		String endName = getEndName();
		html.append("");
		html.append("" + getHiddenCheckBox(startName) + "");
		html.append("<input " + getStyleHtml() + getClassHtml() + " type=\"text\" name=\"" + startName + "\" " + getStyleHtml() + " readonly=\"false\" value='"
				+ defValue + "' /> \n");
		html.append("  到  \n");
		html.append("  " + getHiddenCheckBox(endName) + "");
		html.append("<input " + getStyleHtml() + getClassHtml() + " type=\"text\" name=\"" + endName + "\" " + getStyleHtml() + " readonly=\"false\" value='"
				+ defValue + "' /> \n");
		html.append("   \n");
		html.append("  " + getScript() + " \n");
		return html.toString();
	}

	private String getStartName() {
		return code + "Start";
	}

	private String getEndName() {
		return code + "End";
	}

	private String getScript() {
		StringBuffer html = new StringBuffer(128);
		String startName = getStartName();
		String endName = getEndName();
		String name = startName + "_" + endName;
		String ckbStart = "ckb" + StringUtil.firstCharUpperCase(startName);
		String ckbEnd = "ckb" + StringUtil.firstCharUpperCase(endName);
		if (!checked) {
			html.append("  <script type=\"text/javascript\"> \n");
			html.append("jQuery( function(){ \n");
			html.append("    var selector = \":checkbox[name=" + ckbStart + "],:checkbox[name=" + ckbEnd + "]\"; \n");
			html.append("    jQuery(\"#" + name + "\").click( function(){ \n");
			html.append("        jQuery(selector).attr(\"checked\",this.checked); \n");
			html.append("    }); \n");
			html.append("}); \n");
			html.append("  </script> \n");
		}
		return html.toString();
	}

	/**
	 * 生成CkeckBox
	 * 
	 * @return
	 * @throws Exception
	 */
	private String getHiddenCheckBox(String name) {
		StringBuffer html = new StringBuffer(128);
		String ckbName = "ckb" + StringUtil.firstCharUpperCase(name);
		if (checked)
			html.append("<input type='checkbox' style='display:none' name='" + ckbName + "' value='true' checked='true'/>");
		else
			html.append("<input type='checkbox' style='display:none' name='" + ckbName + "' value=\"true\"/>");
		return html.toString();
	}
}
